package com.yl.chainStore.entity.sick

import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.yl.chainStore.common.annotations.CompanyUserOperationLogColumn
import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.entity.customer.CustomerInfo
import com.yl.chainStore.pojo.vo.customer.CustomerLeftJoinInfo
import com.yl.chainStore.util.timeFormat
import com.yl.common.annotation.NoArg
import com.yl.common.getContext
import java.util.*

/**
 * <p>
 *      客户预约记录
 * </p>
 *
 * @author cxw
 * @since 2021-08-02
 */
@NoArg
@TableName("customer_subscribe_log")
data class CustomerSubscribeLog(

    /**
     * 客户预约记录
     */
    @TableId(value = "id", type = IdType.AUTO)
    var id: Long,

    /**
     * 患者id
     */
    @TableField("customer_id")
    @CompanyUserOperationLogColumn(value = "患者", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_CUSTOMER_INFO)
    var customerId: Long,


    /**
     * 就诊类型
     * //461-480 挂号诊断类型
     */
    @TableField("clinic_type")
    @CompanyUserOperationLogColumn(value = "就诊类型", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_TYPE_ENUM)
    var clinicType: TypeEnum = TypeEnum.SICK_MEDICAL_RECORD_DETAIL_TYPE_FIRST,

    /**
     * 预约类型
     * //551-570 客户预约类型
     */
    @TableField("subscribe_type")
    @CompanyUserOperationLogColumn(value = "预约类型", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_TYPE_ENUM)
    var subscribeType: TypeEnum = TypeEnum.CUSTOMER_SUBSCRIBE_TYPE_NORMAL,

    /**
     * 预约时间
     */
    @TableField("subscribe_time")
    @CompanyUserOperationLogColumn(value = "预约时间", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_DATE_TIME)
    var subscribeTime: Date,

    /**
     * 取消预约时间
     */
    @TableField("cancel_time")
    var cancelTime: Date? = null,

    /**
     * 时长（分钟）
     */
    @TableField("time_length")
    @CompanyUserOperationLogColumn(value = "预约时长", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_TYPE_DEFAULT)
    var timeLength: Int = 30,

    /**
     * 咨询师
     */
    @TableField("consultant_id")
    @CompanyUserOperationLogColumn(value = "咨询师", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_COMPANY_USER)
    var consultantId: Long? = null,

    @TableField(exist = false)
    var consultantName: String? = null,

    /**
     * 科室id
     */
    @TableField("department_id")
    @CompanyUserOperationLogColumn(value = "科室", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_DEPT)
    var departmentId: Long? = null,

    @TableField(exist = false)
    var departmentName: String? = null,

    /**
     * 医生id
     */
    @TableField("doctor_id")
    @CompanyUserOperationLogColumn(value = "医生", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_COMPANY_USER)
    var doctorId: Long? = null,


    /**
     * 医生姓名
     */
    @TableField(exist = false)
    var doctorName: String? = null,

    /**
     * 分院id
     */
    @TableField("sub_company_id")
    var subCompanyId: Long = getContext().subCompanyId,

    @TableField(exist = false)
    var subCompanyName: String? = null,

    /**
     * 诊室id
     */
    @TableField("clinic_id")
    var clinicId: Long? = null,

    /**
     * 护士id
     */
    @TableField("nurse_id")
    @CompanyUserOperationLogColumn(value = "护士", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_COMPANY_USER)
    var nurseId: Long? = null,

    /**
     * 备注
     */
    @TableField("remark")
    @CompanyUserOperationLogColumn(value = "备注", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_TYPE_DEFAULT)
    var remark: String? = null,


    /**
     * 项目id
     */
    @TableField("item_ids")
    @CompanyUserOperationLogColumn(
        value = "预约项目",
        columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_SUBSCRIBE_LOG_ITEMS
    )
    var itemIds: String? = null,

    /**
     * 项目名称
     */
    @TableField(exist = false)
    var itemNames: String? = null,

    @TableField("create_time")
    var createTime: Date = Date(),

    /**
     * 状态：暂时不用
     */
    @TableField("status")
    @CompanyUserOperationLogColumn(value = "状态", columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_STATUS_ENUM)
    var status: Status = Status.NEW_ADD,

    @TableField("history_id")
    var historyId: Long? = null,


    ) : CustomerLeftJoinInfo() {


    var itemIdsShow: List<Long>?
        get() {
            if (itemIds.isNullOrEmpty()) {
                return null
            }
            val split = itemIds!!.split(",")
            val re = mutableListOf<Long>()
            for (s in split) {
                if (s.isEmpty()) {
                    continue
                }
                re.add(s.toLong())
            }
            return re
        }
        set(value) {
            val str = StringBuffer()
            if (!value.isNullOrEmpty()) {
                for (i in value) {
                    str.append("$i,")
                }
            }
            itemIds = str.toString()

        }

    val startTime: String
        get() {
            return timeFormat(subscribeTime)
        }

    val endTime: String
        get() {
            val c = Calendar.getInstance()
            c.time = subscribeTime
            c.add(Calendar.MINUTE, timeLength)
            return timeFormat(c.time)
        }

    val timeInterval: String
        get() {
            return "$startTime-$endTime"
        }

    val haveHistory:Boolean
    get() {
        return historyId != null && historyId != 0L
    }

}
